/*
 * Copyright 2007 National Bioinformatics Network
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package dgbgui.gui.widgets.visual.results;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.geom.Rectangle2D;

import dgbgui.gui.widgets.Highlightable;
import dgbgui.gui.widgets.ShowFrom;
import dgbgui.gui.widgets.ShowSelectable;
import dgbgui.gui.widgets.Widget;
import dgbgui.gui.widgets.visual.RectangleBorder;
import dgbgui.gui.widgets.visual.RectangleWidget;
import dgbgui.modeldocument.primitives.Primitive;
import edu.umd.cs.piccolo.PNode;



@SuppressWarnings("serial")
public class ResultWidget extends RectangleBorder implements Highlightable, ShowSelectable, ShowFrom {
    
    private static final double SELECTABLEPADDING = 5;
    private static enum LineStyle {
        DOTTED,
        LINE
    }
    
    public ResultWidget(PNode parent, Primitive result) {
        super(parent);
        m_result = result;

    }
    
    public Primitive getResult() {
        return m_result;
    }
    
    public void highlight() {
        // For highlighting, store the widget's own fill
        // colour, instead of using the colour that's inherited
        // since that same colour will be restored on unhighlighting
        // (the colour could also be null)
        m_originalFillColor = getOwnFillColour();
        setFillColor(Color.yellow);
    }
    
    public void unhighlight() {
        setFillColor(m_originalFillColor);
    }
    
    public void showFrom() {
        m_fromRectangle = drawAroundWidget(Color.red,LineStyle.LINE);
    }
    
    public void restoreFrom() {
        m_fromRectangle.removeFromParent();
    }
    
    public void showSelectable() {
        m_selectableRectangle = drawAroundWidget(Color.red,LineStyle.DOTTED);
    }
    
    private Widget drawAroundWidget(Color color, LineStyle lineStyle) {
        Rectangle2D bounds = getFullBounds();
        Rectangle2D surround = new Rectangle2D.Double(
                bounds.getX() - SELECTABLEPADDING,
                bounds.getY() - SELECTABLEPADDING,
                bounds.getWidth() + 2.0*SELECTABLEPADDING,
                bounds.getHeight() + 2.0*SELECTABLEPADDING);
        Widget w = new RectangleWidget((Widget)getParent(),surround,5.0,5.0);
        w.setStrokeColor(color);
        if ( lineStyle == LineStyle.DOTTED ) {
            w.setStrokeStyle(new BasicStroke(3, BasicStroke.CAP_ROUND, 
                    BasicStroke.JOIN_ROUND, 0, new float[]{0,6,0,6}, 0));
        } else {
            w.setStrokeStyle(new BasicStroke());
        }
        w.setFillColor(new Color(0,0,0,0));
        w.moveInBackOf(this);
        return w;
    }
    
    public void restoreSelectable() {
        m_selectableRectangle.removeFromParent();
    }
    

    // The result that is represented by this widget
    Primitive m_result;
    
    // The fill color before highlighting
    Color m_originalFillColor;
    
    Widget m_selectableRectangle = null;
    Widget m_fromRectangle = null;
}
